package com.fis.vaadin.component;

import com.vaadin.terminal.Resource;
import com.vaadin.ui.ComponentContainer;
import com.vaadin.ui.MenuBar;
import com.vaadin.ui.MenuBar.MenuItem;
import com.vaadin.ui.Tree;

public class TreeItemId implements MenuBar.Command {
	private Resource icon;
	private String caption;
	private String tooltip;
	private Object value;
	private ComponentContainer relatedForm;
	private Tree tree;
	private String moduleType;

	public String getModuleType() {
		return moduleType;
	}

	public void setModuleType(String moduleType) {
		this.moduleType = moduleType;
	}

	public TreeItemId(Resource icon, String caption, Object value, Tree tree) {
		super();

		if (value == null) {
			throw new IllegalArgumentException(
					"The value of the tree item id cannot be null");
		}

		this.icon = icon;
		this.caption = caption;
		this.value = value;
		this.tree = tree;
	}

	public TreeItemId(Object value, Tree tree) {
		this(null, null, value, tree);
		setCaption(value.toString());
	}

	public Resource getIcon() {
		return icon;
	}

	public void setIcon(Resource icon) {
		this.icon = icon;
	}

	public String getCaption() {
		if (caption == null) {
			setCaption(getValue().toString());
		}
		return caption;
	}

	public void setCaption(String caption) {
		this.caption = caption;
	}

	public String getTooltip() {
		if (tooltip == null) {
			setTooltip(getCaption());
		}

		return tooltip;
	}

	public void setTooltip(String tooltip) {
		this.tooltip = tooltip;
	}

	public Object getValue() {
		return value;
	}

	public void setValue(Object value) {
		this.value = value;
	}

	public ComponentContainer getRelatedForm() throws InstantiationException,
			IllegalAccessException, ClassNotFoundException {

		if (relatedForm == null) {

			String className = value.toString();
			Class<ComponentContainer> obj = (Class<ComponentContainer>) Class
					.forName(className);
			relatedForm = obj.newInstance();

		}

		return relatedForm;

	}

	//
	// public void selectThisItem()
	// {
	// tree.select(this);
	// }

	public String toString() {
		return getValue().toString();
	}

	@Override
	public void menuSelected(MenuItem selectedItem) {
		tree.select(this);
	}
}
